package com.dream.prm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.socket.TextMessage;

import com.dream.prm.common.PortUtils;
import com.dream.prm.common.SpringInit;
import com.dream.prm.socket.MyWebSocketHandler;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * @author w.g
 * @date 2019-07-16
 * 
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/prmAction.do")
public class ProjectManageAction {
	
	@Resource
	MyWebSocketHandler myWebSocketHandler;
	
	/**
	 * TODO 获取端口状态
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping(params="mothod=portStatus")
	public void portStatus(HttpServletRequest req, HttpServletResponse resp)throws IOException {
		String port=SpringInit.prop.get("listener.port")+"";
		String host=SpringInit.prop.get("listener.host")+"";
		String str[]=port.split(",");
		JSONArray jsarr=new JSONArray();
		for(int i=0;i<str.length;i++) {
			if(str[i]!=null&&str[i].length()>0) {
				JSONObject jsonobj=new JSONObject();
				jsonobj.put("port", Integer.valueOf(str[i]));
					try {
						jsonobj.put("status", PortUtils.bindPort(host, Integer.valueOf(str[i])));
					} catch (NumberFormatException e) {
						e.printStackTrace();
					} catch (Exception e) {
						e.printStackTrace();
					}
				jsarr.add(jsonobj);
			}
		}
		//群发消息
		myWebSocketHandler.broadcast(new TextMessage(jsarr.toString()));
		
		resp.getWriter().write("ok");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * TODO 运行bat文件
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping(params="mothod=runBat")
	public void runBat(HttpServletRequest req, HttpServletResponse resp)throws IOException {
		String project_name=req.getParameter("project_name");
		
		String batPath="C:\\Users\\houz\\Desktop\\sctr\\"+project_name+".bat";
		System.out.println(batPath);
		stopTomcat();
		runBat(batPath);
		
		resp.getWriter().write("ok");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * TODO 运行bat文件,启动相应的tomcat
	 * @param batPath
	 */
	public void runBat(String batPath) {
		try {
			
			//Process p = Runtime.getRuntime().exec("cmd /c start  /b "+"E:\\javatools\\tomcat\\启动tomcat-8.0.50.bat");
			Process p = Runtime.getRuntime().exec("cmd /c start  /b "+batPath);
			
			InputStream in = p.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String tmp = null;
			while((tmp=br.readLine())!=null){
				System.out.println(tmp+"");
				if(tmp.contains("start tomcat...")) {
					return;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * TODO tomcat关闭完成
	 * @return
	 * @throws IOException
	 */
	public boolean stopTomcat() throws IOException {
		//关闭其他tomcat,只启动一个tomcat is stoped
		Process stop=Runtime.getRuntime().exec("cmd /c start "+"C:\\Users\\houz\\Desktop\\sctr\\关闭tomcat.bat");
		InputStream in = stop.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String tmp = null;
		while((tmp=br.readLine())!=null){
			System.out.println(tmp+"");
			if(tmp.contains("tomcat is stoped")) {
				return true;
			}
		}
		return false;
		
	}
		
	

	
}
